import {
  EDIT_SHOP_DETAIL_STATUS, 
  SET_GEO_HASH,
  SET_SHOP_INFO,
  SET_SHOP_COMMODITY_LIST,
  NEWLY_ADDED_GOOD,
  ADDONE_TENTH_GOOD,
  EMPTY_GOODS_COUNT,
  SET_USER_INFO,
  EDIT_IMG_PATH,
  SET_ADDRESSES,
  ADD_ADDRESSES,
  DELETE_ADDRESSES,
  EDIT_SEARCH_HISTORY,
  DELETE_SEARCH_HISTORY,
  EMPTY_SEARCH_HISTORY
} from './mutations.type'

export default {
  [EDIT_SHOP_DETAIL_STATUS](state, payload) {
    state.shopDetailStatus = payload
  },
  [SET_GEO_HASH](state, payload) {
    state.geohash = payload
  },
  [SET_SHOP_INFO](state, payload) {
    state.shopInfo = payload
  },
  [SET_SHOP_COMMODITY_LIST](state, payload) {
    state.shopCommodityList = payload
  },
  [NEWLY_ADDED_GOOD](state, payload) {

    //  = state.goods[payload.restaurant_id]必须写如果不写在第一次进入地时候shop category item将没有层级关系
    let shop = state.goods[payload.restaurant_id] = state.goods[payload.restaurant_id] || {};
    let category = shop[payload.category_id] = shop[payload.category_id] || {};
    let item = category[payload.item_id] = category[payload.item_id] || {};
    // let food = item[payload.food_id] || {}=
    item[payload.food_id] = {
      id: payload.id,
      restaurant_id: payload.restaurant_id,
      category_id: payload.category_id,
      item_id: payload.item_id,
      food_id: payload.food_id,
      specs_name: payload.specs_name ? payload.specs_name : '',
      packing_fee: payload.packing_fee,
      price: payload.price,
      name: payload.name,
      count: 1
    }

    state.goods = { ...state.goods }
    localStorage.setItem('buyCart', JSON.stringify(shop))
  },
  [ADDONE_TENTH_GOOD](state, payload) {
    let food = state.goods[payload.restaurant_id][payload.category_id][payload.item_id][payload.food_id]
    food.count++
    state.goods = { ...state.goods }
    localStorage.setItem('buyCart', JSON.stringify(state.goods))
  },
  reduceoneTenthGood(state, payload) {
    let food = state.goods[payload.restaurant_id][payload.category_id][payload.item_id][payload.food_id]
    food.count ? food.count-- : food.count = 0
    state.goods = { ...state.goods }
    localStorage.setItem('buyCart', JSON.stringify(state.goods))

  },
  [EMPTY_GOODS_COUNT](state, payload) {
    state.goods = {}
    localStorage.setItem('buyCart', JSON.stringify(state.goods))
  },
  [SET_USER_INFO](state, payload) {
    state.userInfo = payload
  },
  [EDIT_IMG_PATH](state, payload) {
    if (state.userInfo.avatar) {
      state.userInfo.avatar = payload
    }
  },
  editUserInfoName(state, payload) {
    state.userInfo.username = payload
  },
  [SET_ADDRESSES](state, payload) {
    state.addresses = payload
  },
  [ADD_ADDRESSES](state, payload) {
    state.addresses.unshift(payload)
  },
  [DELETE_ADDRESSES](state, payload) {
    for (const key in state.addresses) {
      state.addresses[key].id === payload ? state.addresses.splice(key, 1) : ''
    }
  },
  [EDIT_SEARCH_HISTORY](state, payload) {
    state.searchHistory.push(payload)
  },
  [DELETE_SEARCH_HISTORY](state, payload) {
    state.searchHistory.splice(payload, 1)
  },
  [EMPTY_SEARCH_HISTORY](state, payload) {
    state.searchHistory = []
  },
  emptyGoodsShopInfo(state, payload) {

    for (const key in state.goods) {
      state.goods[key] = payload === Number(key) ? {} : state.goods[key]
    }
  }
}